// pages/base/user/user.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    token: "",
    baseImgUrl: getApp().globalData.IMG_URL,
    userInfo: {
      realname: "",
      avatar: ""
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    const user = wx.getStorageSync('userInfo');
    console.error(JSON.parse(user));
    this.setData({ userInfo: JSON.parse(user) });

    const token = wx.getStorageSync('token')
    this.setData({ token: token });
  },
  bindInput4Realname: function (e: any) {
    console.error("入参value: " + e.detail.value)
    this.setData({
      'userInfo.realname': e.detail.value
    })
    console.error("入参: " + JSON.stringify(this.data.userInfo))
  },
  /**
   * 头像处理
   */
  chooseMedia() {
    const that = this;
    const preUrl = getApp().globalData.SERVER_URL
    wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
      sourceType: ['album', 'camera'],
      maxDuration: 30,
      camera: 'back',
      success(res) {
        const tempFilePaths = res.tempFiles
        // console.error("res.tempFiles: " + JSON.stringify(tempFilePaths))
        // console.error("开始===========================")
        const url = preUrl + "sys/common/uploadFiles"
        console.error("开始 url:" + url)

        const uploadTask = wx.uploadFile({
          url: url, //仅为示例，非真实的接口地址
          filePath: tempFilePaths[0].tempFilePath,
          name: 'files',
          formData: {
            'biz': 'customer'
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded', // 默认值
            'X-Access-Token': that.data.token
          },
          success(res: any) {
            var data = JSON.parse(res.data);
            if (data.code == 200) {
              // console.error("结果: path = " + JSON.stringify(data.result))
              that.setData({ 'userInfo.avatar': data.result })
            } else {
              console.error(JSON.stringify(data))
            }
            //do something
          }
        })

        // uploadTask.onProgressUpdate((res) => {
        //   // console.log('上传进度', res.progress)
        //   // console.log('已经上传的数据长度', res.totalBytesSent)
        //   // console.log('预期需要上传的数据总长度', res.totalBytesExpectedToSend)
        // })

        // uploadTask.abort() // 取消上传任务

      }
    })
  },
  submitData() {
    const that = this;
    const preUrl = getApp().globalData.SERVER_URL
    const url = preUrl + "sys/user/editUserBaseInfo"
    // console.error("入参: " + JSON.stringify(that.data.userInfo))
    wx.request({
      url: url,
      method: 'PUT',
      data: that.data.userInfo,
      header: {
        // 'content-type': 'application/x-www-form-urlencoded', // 默认值
        'X-Access-Token': that.data.token
      },
      success(res: any) {
        
        wx.stopPullDownRefresh()
        if (res.data.code == 200) {
          that.setData({ 'userInfo': res.data.result })
          wx.setStorageSync('userInfo', JSON.stringify(res.data.result))

          wx.navigateBack()

          // console.error("结果: " + JSON.stringify(that.data.userInfo))
          // const user = wx.getStorageSync('userInfo');
          // console.error("结果2: " + JSON.stringify(user))
        } else {
          console.info(JSON.stringify(res))
        }
      },
    })
  }


})